package com.ljwy.gun.okgoHelper;

import android.app.Activity;
import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Sloop on 2018/8/20
 * Description:
 */
public class HttpRequestHelper {
    /**
     * get  请求
     *
     * @param url
     * @param tag      用于取消网络请求
     * @param map      传参
     * @param callback
     * @param <T>      需要解析的实体类型
     */
    public static <T> void getRequest(Object tag, String url, Map<String, String> map, JsonCallback<T> callback) {
        if (tag instanceof Activity || tag instanceof Context) {
            callback.setContext((Context) tag);
        } else {
            try {
                throw new Exception("HttpRequestHelper参数异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OkGo.<T>get(url)
                .tag(tag)
                .cacheKey(url)
                .retryCount(1)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .cacheTime(60 * 1000)
                .params(map)
                .execute(callback);
    }

    public static <T> void putRequest(Object tag, String url, Map<String, String> map, JsonCallback<T> callback) {
        if (tag instanceof Activity || tag instanceof Context) {
            callback.setContext((Context) tag);
        } else {
            try {
                throw new Exception("HttpRequestHelper参数异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OkGo.<T>put(url)
                .tag(tag)
                .cacheKey(url)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .cacheTime(60 * 1000)
                .params(map)
                .execute(callback);
    }

    public static <T> void getRequest(Object tag, String url, Map<String, String> map, boolean isShowLoading, JsonCallback<T> callback) {
        if (tag instanceof Activity || tag instanceof Context) {
            callback.setContext((Context) tag);
        } else {
            try {
                throw new Exception("HttpRequestHelper参数异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        callback.setShowLoading(isShowLoading);
        OkGo.<T>get(url)
                .tag(tag)
                .cacheKey(url)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .cacheTime(60 * 1000)
                .params(map)
                .execute(callback);
    }

    /**
     * post  请求
     *
     * @param url
     * @param tag      用于取消网络请求
     * @param map      传参
     * @param callback
     * @param <T>      需要解析的实体类型
     */
    public static <T> void postRequest(Object tag, String url, Map<String, String> map, JsonCallback<T> callback) {
        if (tag instanceof Activity || tag instanceof Context) {
            callback.setContext((Context) tag);
        } else {
            try {
                throw new Exception("HttpRequestHelper参数异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OkGo.<T>post(url)
                .tag(tag)
                .params(map)
                .execute(callback);
    }

    /**
     * post  请求
     *
     * @param url
     * @param tag      用于取消网络请求
     * @param map      传参
     * @param callback
     * @param <T>      需要解析的实体类型
     */
    public static <T> void postRequest(Object tag, String url, Map<String, String> map, boolean isSpliceUrl, JsonCallback<T> callback) {
        if (tag instanceof Activity || tag instanceof Context) {
            callback.setContext((Context) tag);
        } else {
            try {
                throw new Exception("HttpRequestHelper参数异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OkGo.<T>post(url)
                .tag(tag)
                .params(map)
                .retryCount(1)
                .isSpliceUrl(isSpliceUrl)
                .execute(callback);
    }

    /**
     * post  请求
     */
    public static <T> void postJsonRequest(Object tag, String url, Map<String, String> map, String json, JsonCallback<T> callback) {
        if (tag instanceof Activity || tag instanceof Context) {
            callback.setContext((Context) tag);
        } else {
            try {
                throw new Exception("HttpRequestHelper参数异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OkGo.<T>post(url)
                .tag(tag)
                .upJson(json)
                .params(map)
                .execute(callback);
    }

    /**
     * post  请求
     *
     * @param url
     * @param tag      用于取消网络请求
     * @param key      图片上传key
     * @param file
     * @param callback
     * @param <T>      需要解析的实体类型
     */
    public static <T> void postSubmitImageRequest(Object tag, String url, String key, File file, JsonCallback<T> callback) {
        if (tag instanceof Activity || tag instanceof Context) {
            callback.setContext((Context) tag);
        } else {
            try {
                throw new Exception("HttpRequestHelper参数异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        List<File> fileDatas = new ArrayList<>();
        fileDatas.add(file);
        OkGo.<T>post(url)
                .isMultipart(true)
                .tag(tag)
                .addFileParams(key, fileDatas)
                .execute(callback);
    }

//    public static void postSubmitImageRequest(Object tag, String url, FileCallback callback) {
//        if (tag instanceof Activity || tag instanceof Context) {
//            callback.setContext((Context) tag);
//        } else {
//            try {
//                throw new Exception("HttpRequestHelper参数异常");
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        OkGo.<File>post(url)
//                .tag(tag)
//                .execute(callback);
//    }

    /**
     * 取消网络请求
     *
     * @param tag
     */
    public static void Cancel(Object tag) {
        OkGo.getInstance().cancelTag(tag);
    }

}
